package com.ruoyi.msg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.msg.domain.MsgFeedback;
import com.ruoyi.msg.domain.bo.MsgFeedbackBo;
import com.ruoyi.msg.domain.vo.MsgFeedbackVo;
import com.ruoyi.msg.mapper.MsgFeedbackMapper;
import com.ruoyi.msg.service.IMsgFeedbackService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 意见反馈Service业务层处理
 *
 * @author strongme
 * @date 2023-04-05
 */
@RequiredArgsConstructor
@Service
public class MsgFeedbackServiceImpl implements IMsgFeedbackService {

    private final MsgFeedbackMapper baseMapper;

    /**
     * 查询意见反馈
     */
    @Override
    public MsgFeedbackVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询意见反馈列表
     */
    @Override
    public TableDataInfo<MsgFeedbackVo> queryPageList(MsgFeedbackBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MsgFeedback> lqw = buildQueryWrapper(bo);
        Page<MsgFeedbackVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询意见反馈列表
     */
    @Override
    public List<MsgFeedbackVo> queryList(MsgFeedbackBo bo) {
        LambdaQueryWrapper<MsgFeedback> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MsgFeedback> buildQueryWrapper(MsgFeedbackBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MsgFeedback> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, MsgFeedback::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), MsgFeedback::getContent, bo.getContent());
        return lqw;
    }

    /**
     * 新增意见反馈
     */
    @Override
    public Boolean insertByBo(MsgFeedbackBo bo) {
        MsgFeedback add = BeanUtil.toBean(bo, MsgFeedback.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改意见反馈
     */
    @Override
    public Boolean updateByBo(MsgFeedbackBo bo) {
        MsgFeedback update = BeanUtil.toBean(bo, MsgFeedback.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MsgFeedback entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除意见反馈
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
